﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace kpi.KpiForm_user
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        Decimal _u_kpi_rat = 0m;
        Decimal _uM_kpi_rec = 0m;

        protected void Page_Load(object sender, EventArgs e)
        {
            Label_UserName.Text = User.Identity.Name;
            if (!Page.IsPostBack)
            {
                //GridView_KpiFormUserRec.DataSource = KpiFormUserRecDataSet(User.Identity.Name);
                //GridView_KpiFormUserRec.DataBind();
            }
        }

        protected String GetSqlConnectionString()
        {
            return "Data Source=192.168.0.241;Initial Catalog=kpi_forms_db;Persist Security Info=True;User ID=sa;Password=My.SA";
        }

        protected SqlConnection GetSqlConnection()
        {
            SqlConnection SqlConnection = new SqlConnection(GetSqlConnectionString());
            return SqlConnection;
        }

        // u_kpi_conf           = 0 用户未确认录入KPI
        // u_kpi_conf           = 1 用户已确认录入KPI
        // u_kpi_conf_datetime  ：用户确认录入时间
        //---------------------------------------------
        // u_kpi_doneconf        = 0 用户未确认KPI完成
        // u_kpi_doneconf        = 1 用户已确认KPI完成
        // u_kpi_done_datetime   : 用户确认KPI完成时间
        //---------------------------------------------
        // uM_kpi_rec    : 用户考评得分
        protected String GetSqlCommand(String commandType, String UserName)
        {
            switch (commandType)
            {
                case "select":
                    return "SELECT [u_kpi] ,[u_kpi_std] ,[u_kpi_comm] ,[u_kpi_rat] ,[u_kpi_fx] ,[u_kpi_rec] ,[uM_kpi_rec] ,[u_kpi_ref],[u_kpi_done_comm] FROM [kpi_forms_db].[dbo].[u_kpi_table] WHERE [Username]='" + UserName + "' AND [u_kpi_ins_datetime]<DATEADD(MONTH, DATEDIFF(MONTH,0,getdate()), 0) AND [u_kpi_conf] = '1'";

                case "insert":
                    return "INSERT INTO u_kpi_table (id, UserName, u_kpi, u_kpi_std, u_kpi_comm, u_kpi_rat, u_kpi_fx, u_kpi_rec, uM_kpi_rec, u_kpi_ref) VALUES "
        + " (@id, @UserName, @u_kpi, @u_kpi_std, @u_kpi_comm, @u_kpi_rat, @u_kpi_fx, @u_kpi_rec, @uM_kpi_rec, @u_kpi_ref)";

                case "update":
                    return "UPDATE [u_kpi_table] SET [u_kpi_doneconf]=@u_kpi_doneconf,[u_kpi_done_datetime]=@u_kpi_done_datetime WHERE [UserName]=@UserName";

                default:
                    return "";
            }
        }

        protected DataSet KpiFormUserRecDataSet(String UserName)
        {
            DataSet UserKpiFormDataSet = new DataSet();

            SqlConnection SqlConnection = GetSqlConnection();
            SqlConnection.Open();

            String selectCommand = GetSqlCommand("select", UserName);

            SqlDataAdapter dataAdapter = new SqlDataAdapter(selectCommand, SqlConnection);
            //
            dataAdapter.Fill(UserKpiFormDataSet);
            //dataAdapter.Fill(UserKpiFormDataTable);

            //GridView_UserKpiForm.DataSource = dataAdapter;
            //GridView_UserKpiForm.DataKeyNames = new string[] { "id" };//主键
            //GridView_UserKpiForm.DataBind();

            dataAdapter.Dispose();

            SqlConnection.Close();

            return UserKpiFormDataSet;
            //return UserKpiFormDataTable;
        }

        // u_kpi_conf           = 0 用户未确认录入KPI
        // u_kpi_conf           = 1 用户已确认录入KPI
        // u_kpi_conf_datetime  : 用户确认KPI录入时间
        //---------------------------------------------
        // u_kpi_doneconf        = 0 用户未确认KPI完成
        // u_kpi_doneconf        = 1 用户已确认KPI完成
        // u_kpi_done_datetime   : 用户确认KPI完成时间
        //---------------------------------------------
        // uM_kpi_rec    : 用户考评得分
        protected void doUserConfUpdate(String UserName)
        {
            SqlConnection SqlConnection = GetSqlConnection();
            SqlConnection.Open();
            SqlCommand cmd = new SqlCommand(GetSqlCommand("update", null), SqlConnection);
            SqlParameter[] SqlParameter = new SqlParameter[3];
            SqlParameter[0] = new SqlParameter("@UserName", SqlDbType.VarChar, 50);
            SqlParameter[1] = new SqlParameter("@u_kpi_doneconf", SqlDbType.NVarChar, 2);
            SqlParameter[2] = new SqlParameter("@u_kpi_done_datetime", SqlDbType.DateTime);
            SqlParameter[0].Value = UserName;
            SqlParameter[1].Value = 1;
            SqlParameter[2].Value = DateTime.Now;

            for (int i = 0; i < SqlParameter.Length; i++)
            {
                cmd.Parameters.Add(SqlParameter[i]);
            }
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
            SqlConnection.Close();

        }

        protected void Button_uConf_Click(object sender, EventArgs e)
        {
            doUserConfUpdate(User.Identity.Name);

            GridView_KpiFormUserRec.Dispose();
            GridView_KpiFormUserRec.DataBind();
        }

        //显示页脚合计
        protected void GridView_UserKpiForm_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView totalRows = (System.Data.DataRowView)e.Row.DataItem;
                for (int i = 0; i < GridView_KpiFormUserRec.HeaderRow.Cells.Count; i++)
                {
                    if (GridView_KpiFormUserRec.HeaderRow.Cells[i].Text.Equals("权重"))
                    {
                        e.Row.Cells[i].HorizontalAlign = HorizontalAlign.Center;
                        _u_kpi_rat += Convert.ToDecimal(totalRows[i].ToString());
                    }
                    else if (GridView_KpiFormUserRec.HeaderRow.Cells[i].Text.Equals("KPI完成数值/内容"))
                    {
                        e.Row.Cells[i].HorizontalAlign = HorizontalAlign.Center;
                        _uM_kpi_rec += Convert.ToDecimal(totalRows[i].ToString());
                    }
                }
            }
            else if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Right;
                e.Row.Cells[2].Text = "合计：";

                for (int i = 0; i < GridView_KpiFormUserRec.HeaderRow.Cells.Count; i++)
                {
                    if (GridView_KpiFormUserRec.HeaderRow.Cells[i].Text.Equals("权重"))
                    {
                        e.Row.Cells[i].Text = _u_kpi_rat.ToString();
                        e.Row.Cells[i].HorizontalAlign = HorizontalAlign.Center;
                    }
                    else if (GridView_KpiFormUserRec.HeaderRow.Cells[i].Text.Equals("KPI完成数值/内容"))
                    {
                        e.Row.Cells[i].Text = _uM_kpi_rec.ToString();
                        e.Row.Cells[i].HorizontalAlign = HorizontalAlign.Center;
                    }
                }
            }
        }
    }
}
